package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCommonMapper;
import com.yuelife.service.pojo.SCommon;
import com.yuelife.service.service.SCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCommon业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCommonServiceImpl implements SCommonService {

    @Autowired
    private SCommonMapper sCommonMapper;


    /**
     * SCommon条件+分页查询
     * @param sCommon 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCommon> findPage(SCommon sCommon, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCommon);
        //执行搜索
        return new PageInfo<SCommon>(sCommonMapper.selectByExample(example));
    }

    /**
     * SCommon分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCommon> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCommon>(sCommonMapper.selectAll());
    }

    /**
     * SCommon条件查询
     * @param sCommon
     * @return
     */
    @Override
    public List<SCommon> findList(SCommon sCommon){
        //构建查询条件
        Example example = createExample(sCommon);
        //根据构建的条件查询数据
        return sCommonMapper.selectByExample(example);
    }


    /**
     * SCommon构建查询对象
     * @param sCommon
     * @return
     */
    public Example createExample(SCommon sCommon){
        Example example=new Example(SCommon.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCommon!=null){
            // 主键评论(自增长)
            if(!StringUtils.isEmpty(sCommon.getCommonId())){
                    criteria.andEqualTo("commonId",sCommon.getCommonId());
            }
            // 店铺id
            if(!StringUtils.isEmpty(sCommon.getStoreId())){
                    criteria.andEqualTo("storeId",sCommon.getStoreId());
            }
            // 评论详情
            if(!StringUtils.isEmpty(sCommon.getContent())){
                    criteria.andEqualTo("content",sCommon.getContent());
            }
            // 星数
            if(!StringUtils.isEmpty(sCommon.getStar())){
                    criteria.andEqualTo("star",sCommon.getStar());
            }
            // 评论用户
            if(!StringUtils.isEmpty(sCommon.getUid())){
                    criteria.andEqualTo("uid",sCommon.getUid());
            }
            // 评论时间
            if(!StringUtils.isEmpty(sCommon.getCreateTime())){
                    criteria.andEqualTo("createTime",sCommon.getCreateTime());
            }
            // 评论订单条目(评论那个订单)
            if(!StringUtils.isEmpty(sCommon.getOrderId())){
                    criteria.andEqualTo("orderId",sCommon.getOrderId());
            }
            // 评论分类
            if(!StringUtils.isEmpty(sCommon.getClassmessage())){
                    criteria.andEqualTo("classmessage",sCommon.getClassmessage());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCommonMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCommon
     * @param sCommon
     */
    @Override
    public void update(SCommon sCommon){
        sCommonMapper.updateByPrimaryKey(sCommon);
    }

    /**
     * 增加SCommon
     * @param sCommon
     */
    @Override
    public void add(SCommon sCommon){
        sCommonMapper.insert(sCommon);
    }

    /**
     * 根据ID查询SCommon
     * @param id
     * @return
     */
    @Override
    public SCommon findById(Integer id){
        return  sCommonMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCommon全部数据
     * @return
     */
    @Override
    public List<SCommon> findAll() {
        return sCommonMapper.selectAll();
    }

    /**
     * 根据店铺id查询所有评论
     * @param storeId
     * @return
     */
    @Override
    public List<SCommon> selectSCommonByStoreId(Integer storeId) {
        return sCommonMapper.selectSCommonByStoreId(storeId);
    }

    /**
     * 根据商品id查询所有的评论
     * @param serviceId
     * @return
     */
    @Override
    public List<SCommon> selectSCommonByserviceId(Integer serviceId) {
        return sCommonMapper.selectSCommonByserviceId(serviceId);
    }

}
